//
//  HomeViewController.m
//  斗鸡TV
//
//  Created by admin on 16/3/10.
//  Copyright © 2016年 admin. All rights reserved.
//

#define HOME_Url @"http://live.9158.com/Fans/GetHotLive"


#import "HomeViewController.h"
#import "ApplicitonClass.h"
#import "HomeModel.h"
#import "HomeViewCell.h"
#import "HeadReusableView.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "LiveViewController.h"
#import "tabbarController.h"
#import "MyLiveViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,LoadHttpDataDelegate>
{
    UICollectionView * collectView;
    HomeModel * sHM;
}

@end

@implementation HomeViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.hidesBarsOnSwipe = YES;

    
    
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(WIDTH/2-2, 130);
    layout.minimumInteritemSpacing = 1;
    
    collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64-48) collectionViewLayout:layout];
    collectView.backgroundColor = [UIColor clearColor];
    collectView.dataSource = self;
    collectView.delegate = self;
    
    [collectView registerClass:[HomeViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectView];

    __weak HomeViewController * weakSelf = self;
    [collectView addPullToRefreshWithActionHandler:^{
        
        [weakSelf laodHttpData];
    }];
    [self laodHttpData];
    
    UIButton * liveBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-100, 20, 100, 40)];
    [liveBtn setTitle:@"我的直播" forState:UIControlStateNormal];
    [liveBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [liveBtn addTarget:self action:@selector(MyLive) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.view addSubview:liveBtn];
    
}

-(void)MyLive
{
    MyLiveViewController * ml = [MyLiveViewController new];
    [[tabbarController getInstance]presentViewController:ml];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  sHM.list.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeViewCell * cell  = [collectView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    SubHomeModel * hm = sHM.list[indexPath.row];
    [cell setCellData:hm];
    
    return cell;
}



-(void)laodHttpData
{
    [self showLoading];
    
    //数据请求（代理方法）
//    ApplicitonClass * applition = [ApplicitonClass getInstance];
//    applition.delegate = self;
//    [applition httpPOSTUrl:HOME_Url params:nil];
    
    //数据请求（Block回调方法）
    [self httpPostUrl:HOME_Url params:nil successBlock:^(NSDictionary *dict) {
        
        if ([dict[@"code"] intValue] == 100)
        {
            NSDictionary * data = [dict dictionaryForKey:@"data"];
            sHM = [[HomeModel alloc]initWithDictionary:data error:nil];
            
        }
        
        [collectView reloadData];
        [collectView.pullToRefreshView stopAnimating];
        [self dismissLoading];
    } errorBlock:^(NSError *error) {
        [collectView.pullToRefreshView stopAnimating];
        
    }];
    
}

//网络请求成功（代理）
-(void)successWithResponse:(id)response
{
    NSDictionary * dict = response;
    if ([dict[@"code"] intValue] == 100)
    {
        NSDictionary * data = [dict dictionaryForKey:@"data"];
        sHM = [[HomeModel alloc]initWithDictionary:data error:nil];
        
    }
    
    [collectView reloadData];
    [collectView.pullToRefreshView stopAnimating];
    [self dismissLoading];
}

//请求失败（代理）
-(void)failedWithResponse:(id)request Error:(NSError *)error
{
    [self showError:[NSString stringWithFormat:@"%@",error]];
     [self dismissLoading];
    NSLog(@"%@",[NSString stringWithFormat:@"%@",error]);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LiveViewController * avPlay = [LiveViewController new];
    SubHomeModel * hm = sHM.list[indexPath.row];
    avPlay.sHM = hm;
    [[tabbarController getInstance] presentViewController:avPlay];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
